package cn.edu.ren.controller;

import cn.edu.ren.pojo.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @PackageName: cn.edu.ren.controller
 * @ClassName: RibbonController
 * @Description:
 * @author: RZS
 * @date: 2021/6/18  21:13
 */
@Api(tags = "负载均衡-RibbonController")
@RestController
@RequestMapping("/ribbon")
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancer;

    @GetMapping("/choose")
    public Object chooseUrl() {
        ServiceInstance instance = loadBalancer.choose("eureka-client");
        return instance;
    }

    @ApiOperation("查询所有student数据接口")
    @GetMapping("/find_all")
    public Collection<Student> findAll(){
        return restTemplate.getForObject("http://eureka-client/student/find_all", Collection.class);
    }

    @GetMapping("/index")
    public String index(){
        return restTemplate.getForObject("http://eureka-client/student/index", String.class);
    }

}
